package com.thm.tfast.modules.sys.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.thm.tfast.core.entity.ViewResult;
import com.thm.tfast.core.enums.DeleteFlagEnum;
import com.thm.tfast.modules.sys.model.SysPermission;
import com.thm.tfast.modules.sys.model.dto.permission.PermissionInsertDto;
import com.thm.tfast.modules.sys.model.dto.permission.PermissionUpdateDto;
import com.thm.tfast.modules.sys.service.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限信息表 前端控制器
 * </p>
 *
 * @author THM
 * @since 2022-10-25
 */
@RestController
@Api(tags = "系统：权限管理")
@RequestMapping("/sysPermission")
public class SysPermissionController {

    @Resource
    private SysPermissionService sysPermissionService;

    @ApiOperation("获取权限列表")
    @GetMapping(value = "/list")
    public ViewResult<List<SysPermission>> list(){
        return ViewResult.success(sysPermissionService.list(Wrappers
                .<SysPermission>lambdaQuery()
                .ne(SysPermission::getDelFlag, DeleteFlagEnum.DELETED.getCode())));
    }

    @ApiOperation("获取指定权限信息")
    @GetMapping("/{permissionId}")
    public ViewResult<SysPermission> getPermissionById(@PathVariable Long permissionId) {
        SysPermission sysPermission = sysPermissionService.getById(permissionId);
        return ViewResult.success(sysPermission);
    }

    @ApiOperation("新增权限信息")
    @PostMapping("/insert")
    public ViewResult<Long> insert(@Validated @RequestBody PermissionInsertDto dto){
        return ViewResult.success(sysPermissionService.insert(dto));
    }

    @ApiOperation("修改权限信息")
    @PutMapping("/update")
    public ViewResult<String> update(@Validated @RequestBody PermissionUpdateDto dto) {
        boolean success = sysPermissionService.update(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("删除权限信息")
    @DeleteMapping("/delete/{permissionId}")
    public ViewResult<String> delete(@PathVariable Long permissionId){
        boolean success = sysPermissionService.delete(permissionId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }
}

